package org.summer.lawyer.controller.wx;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.bo.brokeragerecord.BrokerageRecordBo;
import org.summer.lawyer.domain.bo.userwithdraw.UserWithdrawBo;
import org.summer.lawyer.domain.bo.userwithdraw.WithdrawQrcodeBo;
import org.summer.lawyer.domain.vo.brokeragerecord.BrokerageRecordVo;
import org.summer.lawyer.domain.vo.userwallet.UserWalletVo;
import org.summer.lawyer.domain.vo.userwithdraw.UserWithdrawVo;
import org.summer.lawyer.domain.vo.userwithdraw.WithdrawQrcodeVo;
import org.summer.lawyer.service.IBrokerageRecordService;
import org.summer.lawyer.service.IUserWalletService;
import org.summer.lawyer.service.IUserWithdrawService;

/**
 * 用户钱包
 *
 * @author summer
 * @date 2024-06-27
 */
@Tag(name = "微信小程序用户钱包相关接口", description = "用户钱包")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/wallet")
public class WxUserWalletController {

    private final IUserWalletService userWalletService;

    private final IUserWithdrawService userWithdrawService;

    private final IBrokerageRecordService brokerageRecordService;

    @Parameters({
            @Parameter(name = "wxUserId", description = "微信用户ID", required = true)
    })
    @Operation(summary = "查询我的钱包信息", description = "钱包信息")
    @SaCheckLogin
    @GetMapping("/getWallet/{wxUserId}")
    public R<UserWalletVo> getWallet(@NotNull(message = "用户ID不能为空")@PathVariable("wxUserId")Long wxUserId){
        UserWalletVo vo = userWalletService.selectUserWalletVoByUserId(wxUserId);
        return R.ok(vo);
    }

    @Parameters({
            @Parameter(name = "wxUserId", description = "微信用户ID", required = true),
            @Parameter(name = "pageSize", description = "分页大小", required = true),
            @Parameter(name = "pageNum", description = "当前页数", required = true)
    })
    @Operation(summary = "查询我的收益记录接口", description = "收益记录查询")
    @SaCheckLogin
    @GetMapping("/listBrokerageRecord")
    public TableDataInfo<BrokerageRecordVo> listBrokerageRecord(BrokerageRecordBo bo,  PageQuery pageQuery){
        return brokerageRecordService.selectPageList(bo,pageQuery);
    }

    @Parameters({
            @Parameter(name = "wxUserId", description = "微信用户ID", required = true),
            @Parameter(name = "pageSize", description = "分页大小", required = true),
            @Parameter(name = "pageNum", description = "当前页数", required = true)
    })
    @Operation(summary = "提现记录接口", description = "提现记录查询")
    @SaCheckLogin
    @GetMapping("/listWithdraw")
    public TableDataInfo<UserWithdrawVo> listUserWithdraw(UserWithdrawBo bo, PageQuery pageQuery){
        if(bo.getWxUserId() == null){
            bo.setWxUserId(0L);
        }
        return userWithdrawService.selectPageList(bo,pageQuery);
    }

    @Parameters({
            @Parameter(name = "appId", description = "微信小程序appid", required = true),
            @Parameter(name = "wxUserId",description = "微信用户ID", required = true),
            @Parameter(name = "receivableAccount", description = "收款账户(微信openid,支付宝账号，银行卡号）", required = true),
            @Parameter(name = "amount", description = "提现金额（单位分）不能为空", required = true)
    })
    @Operation(summary = "用户申请提现接口", description = "用户申请提现接口")
    @RepeatSubmit
    @SaCheckLogin
    @PostMapping("/withdraw")
    public R<Void> withdraw(@Validated @RequestBody UserWithdrawBo bo){
        userWithdrawService.applyWithdraw(bo);
        return R.ok("提交成功");
    }

    @Operation(summary = "获取提现二维码图片url", description = "获取提现二维码url")
    @SaIgnore
    @GetMapping("/getWithdrawQrcode")
    public R<WithdrawQrcodeVo> getWithdrawQrcode(WithdrawQrcodeBo bo){
        return R.ok(userWithdrawService.selectWithdrawQrcodeVoById(bo));
    }
}
